package db4o;

import java.util.HashSet;
import java.util.Set;

import com.db4o.ObjectSet;

import pojo.EUserType;
import pojo.User;

public class UserRetriever extends DBManager {
	private ObjectSet<User> result;

	public User fetchUser(final String email) {
		result = super.getDbManager().queryByExample(new User(email));
		if (!result.isEmpty())
			return result.next();
		else
			return null;
	}

	public User fetchUser(String email, String password) {
		User u = this.fetchUser(email);
		if (u != null) {
			if (password.equals(u.getPassword())) {
				return u;
			} else
				return null;
		}
		return null;
	}

	public Set<User> getAllUsers() {
		Set<User> users = new HashSet<User>();
		result = super.getDbManager().queryByExample(User.class);
		for (User d : result) {
			users.add(d);
		}
		return users;
	}

	public Set<User> getUserWhoAre(EUserType type) {
		Set<User> users = new HashSet<User>();
		for (User u : this.getAllUsers()) {
			if (u.getType().equals(type))
				users.add(u);
		}
		return (users==null)? new HashSet<User>() :users ;
	}
}
